﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Asteroids
{
    public class Bullet : SpaceItem
    {
        protected int m_damage;

        public int damage
        {
            get { return m_damage; }
        }

        public new Vector2 centerPosition
        {
            get { return new Vector2(m_position.X + m_sprite.width - radius, m_position.Y + radius); }
            set { m_position = new Vector2(value.X - m_sprite.width + radius, value.Y - radius); }
        }

        public Bullet(int mod, int dam, Vector2 pos, Vector2 vel)
            : base(pos, vel)
        {
            m_damage = dam;
            ItemModel temp = Game1.bulletTypes[0];
            m_maxSpeed = temp.maxSpeed;
            radius = temp.radius;
            m_sprite = new AnimatedSprite(temp.texture, 0, 0, temp.texture.Width, temp.texture.Height, 1);
        }

        public bool collidesWith(SpaceItem item)
        {
            if (item is Enemy)
                return false;
            float slope = m_velocity.Y / m_velocity.X;
            float yInt = centerPosition.Y - slope * centerPosition.X;
            float closestX = (slope * item.centerPosition.Y + item.centerPosition.X - slope * yInt) / (slope * slope + 1);
            float closestY = (slope * slope * item.centerPosition.Y + slope * item.centerPosition.X + yInt) / (slope * slope + 1);
            float distToCollisionX = Math.Abs(centerPosition.X - closestX);
            float distToCollisionY = Math.Abs(centerPosition.Y - closestY);
            float distThisFrameX = Math.Abs(m_velocity.X);
            float distThisFrameY = Math.Abs(m_velocity.Y);
            float dX = Math.Abs(closestX - item.centerPosition.X);
            float dY = Math.Abs(closestY - item.centerPosition.Y);
            if (distToCollisionX < distThisFrameX && distToCollisionY < distThisFrameY && (dX * dX + dY * dY) < item.radius * item.radius)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override void Update(GameTime gametime)
        {
            m_position += m_velocity;
        }
    }
}
